#include "Vector2.h"


Vector2::Vector2(double x, double y) : x_(x), y_(y) {}

Vector2 Vector2::angleToVector(const double angle){
    return Vector2(cos(angle), -sin(angle));
}

const bool Vector2::operator==(const Vector2& other) const{
    return (x_ == other.getX() && y_ == other.getY());
}

inline const bool Vector2::operator!=(const Vector2& other) const{
    return !(*this == other);
}

const Vector2 Vector2::operator+() const{
    Vector2 temp = *this;
    return temp;
}
const Vector2 Vector2::operator-() const{
    Vector2 temp = *this;
    temp.x_ = -temp.x_;
    temp.y_ = -temp.y_;
    return temp;
}

Vector2& Vector2::operator+=(const Vector2& other){
    x_ += other.getX();
    y_ += other.getY();
    return *this;
}
const Vector2 Vector2::operator+(const Vector2& other) const{
    Vector2 temp(*this);
    temp += other;
    return temp;
}

Vector2& Vector2::operator-=(const Vector2& other){
    x_ -= other.getX();
    y_ -= other.getY();
    return *this;
}
const Vector2 Vector2::operator-(const Vector2& other) const{
    Vector2 temp(*this);
    temp -= other;
    return temp;
}

Vector2& Vector2::operator*=(const double s){
    x_ *= s;
    y_ *= s;
    return *this;
}
const Vector2 Vector2::operator*(const double s) const{
    Vector2 temp(*this);
    temp *= s;
    return temp;
}

Vector2& Vector2::operator/=(const double s){
    x_ /= s;
    y_ /= s;
    return *this;
}
const Vector2 Vector2::operator/(const double s) const{
    Vector2 temp(*this);
    temp /= s;
    return temp;
}

//dot product
const double Vector2::operator*(const Vector2& other) const{
    return x_ * other.getX() + y_ * other.getY();
}

//todo: reflect

const double Vector2::magnitude() const{
    return sqrt(pow(x_, 2) + pow(y_, 2));
}

void Vector2::normalize(){
    double m = magnitude();
    x_ /= m;
    y_ /= m;
}

const Vector2 Vector2::normalized() const{
    Vector2 temp = *this;
    temp.normalize();
    return temp;
}

const double& Vector2::getX() const{
    return x_;
}
const double& Vector2::getY() const{
    return y_;
}

const double Vector2::angle() const{
    if (x_ == 0 && y_ == 0)
        return 0;
    return atan2(y_, x_);
}

std::ostream& operator<<(std::ostream& os, const Vector2& v){
    os << "Vector2(" << v.x_ << ", " << v.y_ << ")";
    return os;
}

Vector2 const operator*(const double s, const Vector2& vec){
    return vec * s;
}
